#include "population.h"
#include <stdlib.h>

Population::Population(int n)
{
	_size = n;
	raw = (Genome**) malloc(sizeof(Genome*)*n);
	for(int i = 0; i < n; i++)
	{
		raw[i] = new Genome();
	}
}

Genome& Population::select()
{
	/** selection scheme **/
	return _tournament(16);
}

Genome& Population::_tournament(int tsize)
{
		if(tsize <= 1) {			
			return *raw[rand() % _size];
		}

		Genome& g1 = _tournament(int(tsize/2));
		Genome& g2 = _tournament(int(tsize/2));

		return g1.score > g2.score ? g1 : g2;
}
